package game.Actors;

import java.util.ArrayList;

import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;

/**
 * This class represents an abstract actor in the game.
 * An actor has an image and a position, and
 * everything on the screen that is printed
 * inherits from this class.
 * 
 * @author Sebastian Isheden and Victor Beer
 *
 */
public abstract class Actor {
	private static ArrayList<Actor> actors = new ArrayList<Actor>();
	protected boolean isDead;
	protected Image image;
	protected Vector2f position;
	/**
	 * Creates a new actor with given image and position.
	 * @param image	The image of this Actor
	 * @param position The position of this Actor
	 */
	public Actor(Image image, Vector2f position){
		this.image = image;
		this.position = position;
		actors.add(this);
	}
	/**
	 * Updates the actor.
	 * @param delta	The time in milliseconds since the last update.
	 */
	protected abstract void update(int delta);
	/**
	 * Draws the actor.With a given offset.
	 */
	protected void draw(){
		this.image.draw(position.x - this.image.getWidth()/2, position.y - this.image.getHeight()/2);
		
	}
	/**
	 * Draws all actors on the screen.
	 * All the actors has coordinates with the origin at (0,0). The offset changes this
	 * so the origin is at offset
	 * @param Offset The offset of the new coordinate system.
	 */
	public static void drawAllActors(){
		for(Actor actor : actors){
			actor.draw();
		}
	}
	/**
	 * Updates all actors on the screen.
	 */
	public static void updateAllActors(int delta){
		for(int i = actors.size() -1; i>=0 ; i--){
			actors.get(i).update(delta);
		}
		
		removeDead();
	}
	private static void removeDead() {
		for(int i = actors.size() -1; i>=0 ; i--){
			if(actors.get(i).isDead) {
				actors.get(i).remove();
			}
		}
	}
	/**
	 * This method creates a collision rectangle around the actor.
	 * @param scale The scale we want the rectangle to have, compared to the image.
	 * @return	The collision rectangle.
	 */
	public Rectangle getCollisionRectangle(float scale){
		Rectangle collisionRectangle = new Rectangle(0,0,image.getWidth()*scale,image.getHeight()*scale);
		collisionRectangle.setLocation(new Vector2f(position.x - collisionRectangle.getWidth()/2, position.y - collisionRectangle.getHeight()/2));
		return collisionRectangle;
	}
	/**
	 * Removes the actor from the actor list.
	 */
	protected void remove(){
		actors.remove(this);
	}
	/**
	 * Removes all the actors.
	 */
	public static void clearActors(){
		for(int i = actors.size()-1;i>=0;i--){
			actors.get(i).remove();
		}
	}
}